﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using UtilsORM;
using UtilsORM.MySqlData;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceValueCurrent
    {

      

        public List<iot_device_value_current> getValueCurrentByDevice(string dguid)
        {
            string sql = "select * from iot_device_value_current  where vc_d_guid='" + dguid + "'";
            return MySqlYihongIot.GetList<iot_device_value_current>(sql);
        }

        public DataTable getValueCurrentByProduct(string pguid,string dguid,string modelno)
        {
            string sql = "select  d_guid,d_p_model_no,vc_key,vc_value,vc_add_time from iot_device_value_current c inner join iot_device on d_guid=vc_d_guid  where d_p_guid='" + pguid + "'";
            if(!string.IsNullOrEmpty(dguid))
            {
                sql = sql + " and d_guid='"+dguid+"'";
            }
            if (!string.IsNullOrEmpty(modelno))
            {
                sql = sql + " and d_p_model_no in(" + modelno + ")";
            }
            
            return MySqlYihongIot.GetDataTable(sql);
        }



        public DataTable getDataTableValueCurrent(string dguid)
        {
            string sql = "select  * from iot_device_value_current c left join iot_device_model_variable on v_key=vc_key  where vc_d_guid='" + dguid + "'";
            return MySqlYihongIot.GetDataTable(sql);
        }

        public void setValueCurrentByProduct(string pguid, int modelno, string key, string value)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            iot_device getDevice = ef.iot_device.Where(c => c.d_p_guid == pguid && c.d_p_model_no == modelno).FirstOrDefault();
            if (getDevice != null)
            {
                ef.iot_device_value_current.Where(c => c.vc_d_guid == getDevice.d_guid && c.vc_key == key).ExecuteUpdate(c => c
                  .SetProperty(p => p.vc_value, value)
                  .SetProperty(p => p.vc_add_time, DateTime.Now));
            }
            ef.Dispose();
        }

    }
}
